package com.financing.controller.system;
import java.util.Date;

import com.financing.common.CONST;
import com.financing.model.Data;
import com.financing.model.system.Param;
import com.financing.service.system.ParamService;
import com.financing.util.Json;
import com.financing.util.Str;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class ParamController extends Controller {
	private static ParamService param = new ParamService();

	/**
	 * 保存系统参数
	 * @throws Exception 
	 */ 
	public void edit() throws Exception {
		int id = getParaToInt("id",0);
		String name = getPara("name","");
		String value = getPara("value","");
		if(Str.nb(name) && Str.nb(value)){
			Param pm = id>0 ? Param.me.findById(id) : new Param();
			pm.setName(name);
			pm.setValue(value);
			pm.setSummary(getPara("summary",""));
			CONST.setAttr(name, pm.getValue());  
			if(id>0 ? pm.update() : pm.setCreateTime(new Date()).save()){
		        renderJson(Json.ok("系统参数保存成功！"));
			}else{
		        renderJson(Json.error("系统参数保存失败！"));
			}
		}else{
	        renderJson(Json.error("系统参数不可为空！"));
		}
	}

	/**
	 *  获取系统参数分页数据
	 */
	public void list() throws Exception {
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Page<Param> p = param.list(page, size, Data.flat(getParaMap()));
		renderJson(Json.ok("数据获取成功！", Data.put("page", p)));
	}

	/**
	 *  获取系统参数
	 */
	public void info() throws Exception {
		renderJson(Json.ok("数据获取成功！", Data.put("item", Param.me.findById(getParaToInt("id",0)))));
	}
}
 